#!usr/bin/env python
#coding=utf8

from tuangou.models.tuan_models import Item, ItemImage, TuanGouInfo
from tuangou.common_logic import commutil
from tuangou.business_logic import bi_category

def get_itemtuangou_list(page, page_size, order_by='-tuan_saledate'):
    page, page_size, offset = commutil.get_page_offset(page, page_size)
    tuans = TuanGouInfo.objects.order_by(order_by)[offset : offset + page_size]
    for tuan in tuans:
        tuan.item.price = commutil.decimalFormat(tuan.item.price)
        tuan.tuan_price = commutil.decimalFormat(tuan.tuan_price)
        if len(tuan.tuan_context) > 400:
            tuan.tuan_context = tuan.tuan_context[0:398] + "..."
            
        try:
            tuan.item.homeimage = tuan.item.itemimage_set.filter(imagetype='home')[0]  #ItemImage.objects.get(item_id=item.id, imagetype='home')
        except:
            tuan.item.homeimage = ItemImage(thumburl='', imagetype='home', originalurl='')
            
    return tuans

def get_tuan_count():
    return TuanGouInfo.objects.count()

def get_itemtuan_list_by_item_id(item, page, page_size, order_by='-tuan_saledate'):
    page, page_size, offset = commutil.get_page_offset(page, page_size)
    tuans = item.tuangouinfo_set.order_by(order_by)[offset : offset + page_size]
    for tuan in tuans:
        tuan.item = item
        tuan.tuan_price = commutil.decimalFormat(tuan.tuan_price)
        if len(tuan.tuan_context) > 400:
            tuan.tuan_context = tuan.tuan_context[0:398] + "..."
            
    return tuans, item.tuangouinfo_set.count()

def get_itemtuangou_list_by_category(category, page, page_size, order_by='-tuan_saledate'):
    '''
    按分类来获取团购
    '''
    page, page_size, offset = commutil.get_page_offset(page, page_size)
    order = commutil.get_order_by(order_by)
    childrenIds = bi_category.get_category_ids(category)
    where = ''
    #select = 'item.title,item.price,item.createdtime,tuangouinfo.tuan_sitename,tuangouinfo.tuan_price,tuangouinfo.tuan_salecount,tuangouinfo.tuan_url,tuangouinfo.tuan_saledate,tuangouinfo.supplier_id,tuangouinfo.item_id,tuangouinfo.id AS tuan_id'
    if len(childrenIds) > 0 :
        for id in childrenIds:
            where = where + id + ','
        where = where[0 : len(where) - 1]
    sql = 'SELECT tuangouinfo.* FROM item INNER JOIN itemlinkitemcategory ON item.id=itemlinkitemcategory.item_id AND itemlinkitemcategory.itemcategory_id IN(' + where + ') INNER JOIN tuangouinfo ON item.id=tuangouinfo.item_id ORDER BY %s LIMIT ' + str(offset) + ',' + str(page_size)
    q = TuanGouInfo.objects.raw(sql, [order])
    tuans = []
    for tuan in q:
        tuan.item.price = commutil.decimalFormat(tuan.item.price)
        tuan.tuan_price = commutil.decimalFormat(tuan.tuan_price)
        if len(tuan.tuan_context) > 400:
            tuan.tuan_context = tuan.tuan_context[0:398] + "..."
            
        try:
            tuan.item.homeimage = tuan.item.itemimage_set.filter(imagetype='home')[0]  #ItemImage.objects.get(item_id=item.id, imagetype='home')
        except:
            tuan.item.homeimage = ItemImage(thumburl='', imagetype='home', originalurl='')
        tuans.append(tuan)
    q = TuanGouInfo.objects.raw('SELECT COUNT(1) AS id FROM item INNER JOIN itemlinkitemcategory ON item.id=itemlinkitemcategory.item_id AND itemlinkitemcategory.itemcategory_id IN(' + where + ') INNER JOIN tuangouinfo ON item.id=tuangouinfo.item_id ')
    try:
        tuan_count = q[0].id
    except:
        tuan_count = 0

    return tuans, tuan_count

def get_itemtuangou_by_id(id):
    try:
        tuan = TuanGouInfo.objects.get(id=id)
        tuan.item.price = commutil.decimalFormat(tuan.item.price)
        tuan.tuan_price = commutil.decimalFormat(tuan.tuan_price)
    except:
        tuan = None
    if tuan:
        tuan.item.homeimage = tuan.item.itemimage_set.filter(imagetype='home')[0]
        
    return tuan

def delete_tuangou_by_id(id):
    tuan = TuanGouInfo.objects.get(id=id)
    tuan.delete()

def save_tuangou(data):
    t = TuanGouInfo()
    t.tuan_salecount = data['tuan_salecount']
    t.tuan_url = data['tuan_url']
    t.tuan_saledate = data['tuan_saledate']
    t.tuan_price = data['tuan_price']
    t.tuan_sitename = data['tuan_sitename']
    t.item_id = data['item_id']
    t.supplier_id = data['supplier_id']
    t.tuan_context = data['tuan_context']
    t.save()
    return t

def update_tuangou(id, data):
    t = TuanGouInfo.objects.get(id=id)
    t.tuan_salecount = data['tuan_salecount']
    t.tuan_url = data['tuan_url']
    t.tuan_saledate = data['tuan_saledate']
    t.tuan_price = data['tuan_price']
    t.tuan_sitename = data['tuan_sitename']
    t.supplier_id = data['supplier_id']
    t.tuan_context = data['tuan_context']
    t.save()
    return t

    